/*
JETware Hands-free Extension provides additional features for Windows Mobile HFP support.
Copyright (C) 2004-2009 Jim Thatcher

This program is free software; you can redistribute it and/or modify it under the terms of the 
GNU General Public License as published by the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; 
if not, write to the Free Software Foundation, Inc., 
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

Source Control header

$Workfile: DebugLog.cpp $
$Revision: 10 $
$Date: 2/03/07 12:47p $
*/
#include "stdafx.h"
#include <windows.h>
#include "BTHFExt.h"

DWORD g_dwDebugLevels = 0x0;

void LogString(LPSTR szCommand, DWORD dwLevel)
{
	if (dwLevel & g_dwDebugLevels)
	{
		SYSTEMTIME st;
		HANDLE h = NULL;
		char szTime[16];
		TCHAR szPath[MAX_PATH];
		HMODULE hMod = NULL;
		DWORD dwRet;

		lstrcpy(szPath, _T("JetBTHFx.dll")),		
		hMod = GetModuleHandle(szPath);
		dwRet = GetModuleFileName(hMod, szPath, MAX_PATH);

		if (dwRet)
		{
			TCHAR  *cSlash = wcsrchr(szPath, '\\');
			*cSlash = 0;
			lstrcat(szPath, L"\\BTAGATLog.log");
		}
		else
		{
			BOOL bRet = SHGetSpecialFolderPath(NULL, szPath, CSIDL_PROGRAM_FILES, FALSE);
			lstrcat(szPath, L"\\JETware\\BTAGATLog.txt");
		}


		h = CreateFile(szPath, GENERIC_WRITE, 0,
			NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
		GetLocalTime(&st);
		if (dwLevel == LOG_REQUESTS)
			sprintf(szTime, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d:%2.2d <-\t", st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond);
		else
			sprintf(szTime, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d:%2.2d ->\t\t", st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond);
		if (h != INVALID_HANDLE_VALUE)
		{
			BOOL bResult;
			dwRet = SetFilePointer(h, 0, NULL, FILE_END);
			if (g_dwDebugLevels & DEBUG_THREADING)
			{
				char szThread[16];
				sprintf(szThread, "(%8.8X) ", GetCurrentThreadId());
				bResult = WriteFile(h, szThread, strlen(szThread), &dwRet, NULL);
			}
			bResult = WriteFile(h, szTime, strlen(szTime), &dwRet, NULL);
			bResult = WriteFile(h, szCommand, strlen(szCommand), &dwRet, NULL);
			bResult = WriteFile(h, "\r\n", 2, &dwRet, NULL);
			CloseHandle(h);
		}
	}
}

